import {getSetting} from '../api/setting'
import titleController from '../utils/titleController';

export default {
  namespaced: true, //开启命名空间
  state:{
    loading:false,
    data:null
  },
  mutations:{
    setLoading(state,palyod){
      state.loading = palyod;
    },
    setData(state,palyod){
      state.data = palyod;
    },
  },
  actions:{
    async fetchSetting(ctx){
      ctx.commit("setLoading",true);
      const resp = await getSetting();
      ctx.commit("setData",resp);
      ctx.commit("setLoading",false)
      if(resp.favicon){
        let link = document.querySelector("link[ref='shortcut icon']");
        if(link){
          return;
        }
          link = document.createElement("link");
          link.rel = "shortcut icon";
          link.type = "images/x-icon";
          link.href = resp.favicon;
          document.querySelector("head").appendChild(link)
      }

      if(resp.siteTitle){
        titleController.setSiteTitle(resp.siteTitle);
      }
    }
  }
}